package com.smarthome.web.property.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.core.user.UserResourceService;
import com.smarthome.core.user.UserRoleService;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.bean.ShUser;
import com.smarthome.domain.bean.ShUserRole;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.ShUserResourceTypeFlagEnum;
import com.smarthome.domain.vo.boss.Tree;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 角色控制器
 * 
 * @author KongXiangyu
 * 
 */
@Controller
@RequestMapping("/roleController")
public class RoleController {

    @Resource
    private UserRoleService userRoleService;
    
    @Resource(name="residenceManagerService")
    private ResidenceManagerService residenceManagerService;
    
    @Resource(name="userResourceService")
    private UserResourceService resourceService;
    
    boolean isAdmin = true;

    /**
     * @title 跳转到页面展示
     * @return
     */
    @RequestMapping("/views")
    public String views(HttpServletRequest request){
    	isAdmin = UserHolder.isSuperResidence();
    	request.setAttribute("isAdmin",isAdmin);
		
		if(UserHolder.isSuperResidence()){
			request.setAttribute("residentces",residenceManagerService.queryShResidences(null));
		} else {
			ShResidence residence = new ShResidence();
			residence.setId(UserHolder.getCurrentResidenceId());
			//设置当前小区
			request.setAttribute("residence", residence);
		}
    	
    	
    	return "/system/role";
    }
    
    /**
     * 跳转到角色添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
    	
    	//获取上级角色ID
    	request.setAttribute("parentId", request.getParameter("id"));
    	Long residenceId =null;
    	try{
    		 residenceId = Long.parseLong(request.getParameter("residenceId"));
    	} catch(Exception e){
    		residenceId = UserHolder.getCurrentResidenceId();
    	}
    	isAdmin = UserHolder.isSuperResidence();
    	request.setAttribute("isAdmin", isAdmin);
    	List<Tree> trees = null;
    	if(isAdmin){
    		trees = userRoleService.treeByResidenceId(null);
    		//加载小区列表
    		request.setAttribute("residences", residenceManagerService.queryShResidences(null));
    		 residenceManagerService.queryShResidences(null);
    	} else {
    		trees = userRoleService.treeByResidenceId(UserHolder.getCurrentResidenceId());
    		request.setAttribute("residence", residenceManagerService.queryResidenceById(residenceId));
    	}
    	
    	request.setAttribute("residenceId", residenceId );
    	request.setAttribute("parentId", residenceId );
    	List<Tree> resouceTrees = resourceService.tree(UserHolder.getUser(),ShUserResourceTypeFlagEnum.MENU );
    	//设置资源菜单
    	request.setAttribute("resouceTrees", JSONObject.toJSON(resouceTrees));
    	//角色管理
    	request.setAttribute("roleTrees", JSONObject.toJSON(trees));
        return "/system/roleAdd";
    }

    /**
     * 添加角色
     * 
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ShUserRole role) {
    	if(null == role.getResidenceId()){
    		return Result.failure("请选择一个小区进行添加");
    	}
    	
    	if(null == role.getParentId()){
    		return Result.failure("请选择一个角色上级名称");
    	}
    	//数据验证
    	if(StringUtils.isBlank(role.getName())){
    		return Result.failure("角色名称不能为空");
    	}
    	
    	if(userRoleService.isRepeat(role)){
    		return Result.failure("角色名称重复");
    	}
    	
        userRoleService.add(role);
        return Result.success("添加成功！");
    }

    /**
     * 跳转到角色修改页面
     * 
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, Long id) {
    	ShUserRole role = userRoleService.queryPropertyRoleById(id);
        request.setAttribute("role", role);
    	List<Tree> resouceTrees = resourceService.tree(UserHolder.getUser(),ShUserResourceTypeFlagEnum.MENU );
    	//设置资源菜单
    	request.setAttribute("resouceTrees", JSONObject.toJSON(resouceTrees));
        return "/system/roleEdit";
    }

    /**
     * 修改角色
     * 
     * @param role
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(ShUserRole role) {
    	if(null == role.getResidenceId()){
    		return Result.failure("请选择一个小区进行添加");
    	}
    	
    	if(null == role.getParentId()){
    		return Result.failure("请选择一个角色上级名称");
    	}
    	//数据验证
    	if(StringUtils.isBlank(role.getName())){
    		return Result.failure("角色名称不能为空");
    	}
    	
    	if(userRoleService.isRepeat(role)){
    		return Result.failure("角色名称重复");
    	}
    	
        userRoleService.edit(role);
        return Result.success("编辑成功！");
    }

    /**
     * 获得角色列表
     * 
     * @return
     */
    @RequestMapping("/treeGrid")
    @ResponseBody
    public List<ShUserRole> treeGrid(ShUserRole role) {
    	
        return userRoleService.treeGrid(UserHolder.getUser(),role);
    }

    /**
     * 角色树(只能看到自己拥有的角色)
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree(HttpServletRequest request) {
    	//ShUser u =  UserHolder.getShPropertyUser();
        return userRoleService.tree(UserHolder.getUser());
    }

    /**
     * 角色树
     * 
     * @return
     */
    @RequestMapping("/allTree")
    @ResponseBody
    public List<Tree> allTree() {
        return userRoleService.tree(null);
    }

    /**
     * @title  根据小区id，获取小区下的树
     * @createTime 2015年9月22日 下午7:37:13
     * @author dhc
     */
    @RequestMapping("/residenceTree")
    @ResponseBody
    public List<Tree> treeResidentce(HttpServletRequest request,Long residenceId){
    	//queryShResidences
    	return  userRoleService.treeByResidenceId(residenceId);
    }
    
    /**
     * 删除角色
     * 
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Long id) {
        userRoleService.delete(id);
        return Result.success("删除成功！");
    }

    /**
     * 跳转到角色授权页面
     * 
     * @return
     */
    @RequestMapping("/grantPage")
    public String grantPage(HttpServletRequest request, Long id) {
        request.setAttribute("role", userRoleService.queryPropertyRoleById(id));
        return "/system/roleGrant";
    }

    /**
     * 授权
     * 
     * @param role
     * @return
     */
    @RequestMapping("/grant")
    @ResponseBody
    public Result grant(ShUserRole role) {
        userRoleService.grant(role);
        return Result.success("授权成功！");
    }
    
    

}
